package com.bw.educate.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bw.educate.common.ResponseResult;
import com.bw.educate.entity.EduTeacher;
import com.bw.educate.service.IEduTeacherService;
import com.bw.educate.util.TestImport;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 在线教育-五组
 * @since 2020-08-12
 */
@RestController
@RequestMapping("eduTeacher")
public class EduTeacherController {

    @Autowired
    private IEduTeacherService eduTeacherService;

    @Autowired
    private TestImport testImport;

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @Value(("${filePath}"))
    private String filePath;
    /**
     * @Description:讲师列表查询
     * @Author: 周明丽
     * @Date: 2020/8/14 8:28

     * @return: com.bw.educate.common.ResponseResult
     **/
    @PostMapping("list/{currentPage}/{pageSize}")
    public ResponseResult list(@PathVariable("currentPage") int currentPage, @PathVariable("pageSize") int pageSize, @RequestBody EduTeacher eduTeacher){
        ResponseResult result=ResponseResult.SUCCESS();
        try {

            PageHelper.startPage(currentPage,pageSize);
            QueryWrapper queryWrapper=new QueryWrapper();

            if(null!=eduTeacher.getFullName()){
                queryWrapper.like("USER_NAME","%"+eduTeacher.getFullName()+"%");
            }

            queryWrapper.eq("IS_DELETE",0);
            List<EduTeacher> list=eduTeacherService.list(queryWrapper);
            PageInfo pageInfo = new PageInfo(list);
            result.setResult(pageInfo);
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("讲师列表查询失败"+e.toString());
        }

        return result;
    }

    /**
     * @Description:讲师添加
     * @Author: 周明丽
     * @Date: 2020/8/14 8:44
     * @param eduTeacher:
     * @return: com.bw.educate.common.ResponseResult
     **/
    @PostMapping("addSave")
    public ResponseResult addSave(@RequestBody EduTeacher eduTeacher){
        ResponseResult result=ResponseResult.SUCCESS();
        try {
            eduTeacher.setIsDelete(0);
            eduTeacher.setGenerate(0);
            eduTeacherService.saveOrUpdate(eduTeacher);
            redisTemplate.opsForHash().put("teacherList",String.valueOf(eduTeacher.getId()),eduTeacher);//添加同步添加到缓存
            result.setMessage("保存成功");
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("讲师保存失败"+e.toString());
        }
        return result;
    }

    /**
     * @Description:删除讲师
     * @Author: 周明丽
     * @Date: 2020/8/14 8:56
     * @param id:
     * @return: com.bw.educate.common.ResponseResult
     **/
    @GetMapping("delUser/{id}")
    public ResponseResult del(@PathVariable("id") Long id){

        ResponseResult result=ResponseResult.SUCCESS();

        try {
            EduTeacher byId = eduTeacherService.getById(id);
            byId.setIsDelete(1);
            eduTeacherService.saveOrUpdate(byId);
            redisTemplate.opsForHash().delete("teacherList",String.valueOf(byId.getId()));//删除缓存
            result.setMessage("删除成功");

        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("讲师删除失败"+e.toString());
        }

        return result;
    }

    /**
     * @Description:根据id查询讲师
     * @Author: 周明丽
     * @Date: 2020/8/14 9:01
     * @param id:
     * @return: com.bw.educate.common.ResponseResult
     **/
    @GetMapping("findTeacher/{id}")
    public ResponseResult findTeacher(@PathVariable("id") Long id){

        ResponseResult result=ResponseResult.SUCCESS();
        try {
            result.setResult(eduTeacherService.getById(id));
            result.setMessage("查询成功");

        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("讲师查询失败"+e.toString());
        }
        return result;
    }

    /**
     * @Description:修改讲师
     * @Author: 周明丽
     * @Date: 2020/8/14 9:04
     * @param eduTeacher:
     * @return: com.bw.educate.common.ResponseResult
     **/
    @GetMapping("updateTeacher")
    public ResponseResult updateTeacher(@RequestBody EduTeacher eduTeacher){

        ResponseResult result=ResponseResult.SUCCESS();
        try {
            eduTeacherService.updateById(eduTeacher);
            result.setMessage("修改成功");

        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("讲师修改失败"+e.toString());
        }
        return result;
    }

    /**
     * @Description:讲师图片上传
     * @Author: 周明丽
     * @Date: 2020/8/15 21:57
     * @param file:
     * @return: com.bw.educate.common.ResponseResult
     **/
    @PostMapping("upload")
    public ResponseResult upload(MultipartFile file){

        ResponseResult result=ResponseResult.SUCCESS();
        try {
            Map map = testImport.upload(file);
            result.setResult(map);
            result.setMessage("上传成功");
        }catch (Exception e){
            e.printStackTrace();
            result.setSuccess(false);
            result.setMessage("学员头像上传失败"+e.toString());
        }
        return result;
    }

    /**
     * @Description:讲师图片下载
     * @Author: 周明丽
     * @Date: 2020/8/15 21:57
     * @param path:
     * @return: com.bw.educate.common.ResponseResult
     **/
    @GetMapping("download/{path}")
    public void download(@PathVariable("path") String path, HttpServletResponse response){

        try {
            testImport.downLoad(path,response);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * @Description:课程下拉框展示讲师列表
     * @Author: 王文佳
     * @Date: 2020/8/16 22:07
     * @return: com.bw.educate.common.ResponseResult
     **/
    @GetMapping("/teacherList")
    public ResponseResult teacherList(){

        return eduTeacherService.teacherList();
    }

    /**
     * @Description:讲师C端页面展示
     * @Author: 王文佳
     * @Date: 2020/8/20 22:48
     * @return: com.bw.educate.common.ResponseResult
     **/
    @GetMapping("/findTeachers")
    public ResponseResult teacherShowList(){

        return eduTeacherService.teacherShowList();
    }

    /**
     * @Description:将添加好的讲师生成静态页
     * @Author: 王文佳
     * @Date: 2020/8/24 20:18
     * @param id:
     * @return: com.bw.educate.common.ResponseResult
     **/
    @GetMapping("generatePage/{id}")
    public ResponseResult generatePage(@PathVariable("id") Long id){

        return eduTeacherService.generatePage(id);
    }

    /**
     * @Description:删除生成的静态页面
     * @Author: 王文佳
     * @Date: 2020/8/25 14:34
     * @param id:
     * @return: com.bw.educate.common.ResponseResult
     **/
    @GetMapping("delPage/{id}")
    public ResponseResult delPage(@PathVariable("id") Long id){

        return eduTeacherService.delPage(id);
    }

    @GetMapping("findTeacherById/{id}")
    public EduTeacher findTeacherById(@PathVariable("id") Long id){

        return eduTeacherService.getById(id);
    }

}
